<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.keyword"
        placeholder="请输入关键字"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-date-picker
        v-model="listQuery.dateRange"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        class="filter-item"
        style="width: 360px"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="success"
        icon="el-icon-s-data"
        @click="handleStats"
      >
        统计
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column
        label="员工姓名"
        prop="employeeName"
        align="center"
        width="120"
      />
      <el-table-column
        label="部门"
        prop="departmentName"
        align="center"
        width="150"
      />
      <el-table-column
        label="日期"
        prop="date"
        align="center"
        width="120"
      />
      <el-table-column
        label="上班时间"
        prop="checkInTime"
        align="center"
        width="120"
      />
      <el-table-column
        label="下班时间"
        prop="checkOutTime"
        align="center"
        width="120"
      />
      <el-table-column
        label="工作时长"
        prop="workHours"
        align="center"
        width="120"
      />
      <el-table-column
        label="状态"
        prop="status"
        align="center"
        width="100"
      >
        <template #default="{row}">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        prop="remark"
        align="center"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default defineComponent({
  name: 'AttendanceList',
  components: { Pagination },
  directives: { waves },
  setup() {
    const list = ref([])
    const total = ref(0)
    const listLoading = ref(true)
    const listQuery = ref({
      page: 1,
      limit: 20,
      keyword: '',
      dateRange: []
    })

    const getStatusType = (status) => {
      const types = {
        normal: 'success',
        late: 'warning',
        early: 'warning',
        absent: 'danger'
      }
      return types[status] || 'info'
    }

    const getStatusText = (status) => {
      const texts = {
        normal: '正常',
        late: '迟到',
        early: '早退',
        absent: '缺勤'
      }
      return texts[status] || '未知'
    }

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取考勤数据
      setTimeout(() => {
        list.value = [
          {
            employeeName: '张三',
            departmentName: '技术部',
            date: '2024-01-01',
            checkInTime: '09:00',
            checkOutTime: '18:00',
            workHours: '9',
            status: 'normal',
            remark: ''
          }
        ]
        total.value = 1
        listLoading.value = false
      }, 500)
    }

    const handleFilter = () => {
      listQuery.value.page = 1
      getList()
    }

    const handleStats = () => {
      // TODO: 跳转到考勤统计页面
    }

    onMounted(() => {
      getList()
    })

    return {
      list,
      total,
      listLoading,
      listQuery,
      getStatusType,
      getStatusText,
      handleFilter,
      handleStats
    }
  }
})
</script>